from redis import ConnectionPool, StrictRedis

from .log import logger
from config import RedisConfig, HOSTS_DICT


class Redis:

    def __init__(self, redis_config=RedisConfig):
        pool = ConnectionPool(
            host=redis_config.host,
            port=redis_config.port,
            db=redis_config.db,
            password=redis_config.password,
        )
        self.redis = StrictRedis(connection_pool=pool, decode_responses=True)
        if self.redis.ping():
            logger.success(f'HOST:{HOSTS_DICT[redis_config.host]} redis连接成功!  {self.redis.connection_pool}')
        else:
            logger.error(f'HOST:{HOSTS_DICT[redis_config.host]} redis连接失败!')


if __name__ == '__main__':
    class RedisConfig:
        host = '47.242.154.37'
        port = 6380
        db = 1
        password = 'redis_on_HK61'


    redis = Redis(RedisConfig).redis
